
package cj.web.activity.controllers;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.web.activity.dao.PrizeResultDAO;
import cj.web.activity.domain.PrizeResult;
import cj.web.activity.utils.Constants;
import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

@Path("/prize")
public class PrizeAdminController{

	private static Logger logger = Logger.getLogger(PrizeAdminController.class.getName());
	
	
	private int mStart=0;
	private int mIndex=0;
	private String mActive="";
	
	
	@Autowired
	private PrizeResultDAO prizeResultDAO;
	
	@Get("admin")
	public String admin()
	{
        return "/views/activity/prizeList.vm";
	}
	
	@Get("goGiftByDeviceId")
	public String goGiftByDeviceId(Model model,@Param("deviceId") String deviceId,
			@Param("activeId") int activeId)
	{
		PrizeResult obj = new PrizeResult();
		obj.setDeviceID(deviceId);
		obj.setActiveID(activeId);
//		obj.set
		return null;
	}
	
	@Get("addAM")
	public String addAM(Model model,@Param("activeId") int activeId
			,@Param("phone") String phone ,@Param("deviceId") String deviceId  
			,@Param("deviceType") int deviceType ,@Param("status") int status  	,@Param("remark") String remark )
	{
		try {
			PrizeResult obj = new PrizeResult();
			obj.setActiveID(activeId);
			obj.setPhone(phone);
			obj.setDeviceID(deviceId);
			obj.setDeviceType(String.valueOf(deviceType));
			obj.setStatus(status);
			obj.setRemark(remark);
			prizeResultDAO.create(obj);
			return "0";
		} catch (Exception e) {
			return "-1";
		}
	}	
	
	@Post("queryAll")
	public String queryAll(@Param("start") int start,@Param("length") int length )
	{
			JSONObject jopublic =new JSONObject();
			try {
				jopublic.put("returnFlag", 0);
				jopublic.put("returnMsg", "成功");
				mStart = start;
				start++;
				List<PrizeResult> objs = 	prizeResultDAO.queryRSLimit(start-1, length,Constants.Activity.ActivityId_5);
				//List<ActiveManagement> objs = 	activeManagementDao.queryRSLimit(0, 10);
				List<Map> reps = new ArrayList<Map>();
				for(PrizeResult obj:objs)
				{
					Map rep = new HashMap();
					rep.put("Phone", obj.getPhone());
					rep.put("DeviceID", obj.getDeviceID());
					rep.put("CreateTime", obj.getCreateAT());
					rep.put("Status", GetState(obj.getStatus()));
					int t =Integer.parseInt( obj.getRemark());
					rep.put("Remark",  Winning(t)  );
					mActive = obj.getActiveID()+""  ;
					reps.add(rep);
				}
				jopublic.put("data", reps);		
				int conut =prizeResultDAO.queryRSTotal(Constants.Activity.ActivityId_5);
				if (conut==0) {
					conut++;
				}
				jopublic.put("recordsTotal", conut);
				jopublic.put("recordsFiltered", conut);
			} catch (Exception e) {
				jopublic.put("returnFlag", -1);
				jopublic.put("returnMsg", "失败");
			}
			return  "@"+jopublic.toString();
	}	
	
	@Get("modAM")
	public String modAM(@Param("phone") String phone ,@Param("deviceId") String deviceId  ,@Param("status") int status  ,@Param("remark") String remark)
	{
		JSONObject jopublic =new JSONObject();
		try {
			jopublic.put("returnFlag", 0);
			jopublic.put("returnMsg", "成功");
			prizeResultDAO.updateState(deviceId ,phone,status,Constants.Activity.ActivityId_5);
			if(status==2)
			{
				prizeResultDAO.updateCount(remark ,mActive);
			}
		} catch (Exception e) {
			jopublic.put("returnFlag", -1);
			jopublic.put("returnMsg", "失败");
		}
		return  "@"+jopublic.toString();
	}
	
	@Post("queryAM")
	public String queryAM(@Param("start") int start,@Param("length") int length ,@Param("temp") String temp )
	{
		JSONObject jopublic =new JSONObject();
		try {
			start++;
			List<PrizeResult> objs = 	prizeResultDAO.queryBypd(start-1, length,temp,Constants.Activity.ActivityId_5);
			jopublic.put("returnFlag", 0);
			jopublic.put("returnMsg", "成功");
			List<Map> reps = new ArrayList<Map>();
			for(PrizeResult obj:objs)
			{
				Map rep = new HashMap();
				rep.put("Phone", obj.getPhone());
				rep.put("DeviceID", obj.getDeviceID());
				rep.put("CreateTime", obj.getCreateAT());
				rep.put("Status", GetState(obj.getStatus()));
				int t =Integer.parseInt( obj.getRemark());
				rep.put("Remark",  Winning(t)  );
				mActive = obj.getActiveID()+""  ;
				reps.add(rep);
			}
			jopublic.put("data", reps);		
			int conut =prizeResultDAO.queryRSTotalBypd(temp,Constants.Activity.ActivityId_5);
			if (conut==0) {
				conut++;
			}
			jopublic.put("recordsTotal", conut);
			jopublic.put("recordsFiltered", conut);
		} catch (Exception e) {
			jopublic.put("returnFlag", -1);
			jopublic.put("returnMsg", "失败");
		}
		return  "@"+jopublic.toString();
	}
	
	public String Winning(int remark)
	{
		String temp="";
		switch (remark) {
		case 1:
			temp="iphone6s";
			break;
		case 2:
			temp="5L装花生油";
			break;
		case 3:
			temp="5kg装大米";
			break;
		case 4:
			temp="品牌洗发水/精华素";
			break;
		case 5:
			temp="精美工艺品";
			break;
		case 6:
			temp="便民手推车";
			break;
		case 7:
			temp="品牌手表";
			break;
		default:
			temp="未中奖";
			break;
		}
		return temp;
	}
	
	public String GetState(int state)
	{
		String temp="";
		switch (state) {
		case 0:
			temp="未领奖";
			break;
		case 1:
			temp="已领奖";
			break;
		case 2:
			temp="已无效";
			break;
		case -1:
			temp="未中奖";
			break;
		default:
			temp="";
			break;
		}
		return temp;
	}
}